package com.vmladenov.cook.core;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.vmladenov.cook.R;
import com.vmladenov.cook.core.db.proxy.RecipeComment;
import com.vmladenov.cook.core.html.Recipe;
import com.vmladenov.cook.ui.user.LoginActivity;
import com.vmladenov.cook.ui.user.RegisterActivity;

public final class DialogHelper
{
	public static void showAlerDialog(Context context, String title, String message)
	{
		new AlertDialog.Builder(context).setMessage(message).setTitle(title).setPositiveButton(context.getText(R.string.ok), null).create().show();
	}

	public static void showErrorDialog(Context context, String message)
	{
		showAlerDialog(context, context.getString(R.string.error), message);
	}

	public static void showViewRecipeInfo(Activity activity)
	{
		final SharedPreferences preferences = activity.getSharedPreferences("MandjaSettings", Context.MODE_PRIVATE);
		Boolean firtTimeShow = preferences.getBoolean("HasShowRecipeViewInfo", false);
		if (!firtTimeShow)
		{
			LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.view_recipe_info, null);
			final Dialog dialog = new Dialog(activity);
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(layout);
			layout.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					Editor editor = preferences.edit();
					editor.putBoolean("HasShowRecipeViewInfo", true);
					editor.commit();
					dialog.dismiss();
				}
			});
			dialog.getWindow().setGravity(Gravity.CENTER);

			dialog.show();
		}
	}

	public static void showAbout(final Activity activity)
	{
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.about, null);

		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setView(layout);
		final AlertDialog alertDialog = builder.create();

		TextView txtTitleVersion = (TextView) layout.findViewById(R.id.txtTitleVersion);
		try
		{
			PackageInfo packageInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
			txtTitleVersion.setText(String.format(activity.getString(R.string.about_header), packageInfo.versionName));
		}
		catch (Exception e)
		{
		}

		TextView txtEmail = (TextView) layout.findViewById(R.id.txtAboutEmail);
		txtEmail.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				/* Create the Intent */
				final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

				/* Fill it with Data */
				emailIntent.setType("plain/text");
				emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "ventsislav.mladenov@gmail.com" });
				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
				emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");

				/* Send it off to the Activity-Chooser */
				activity.startActivity(emailIntent);
				// startActivity(Intent.createChooser(emailIntent,
				// "Send mail..."));
			}
		});

		TextView txtBrowse = (TextView) layout.findViewById(R.id.txtSite);
		txtBrowse.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent viewSiteIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.gotvetesmen.com/"));
				activity.startActivity(viewSiteIntent);
			}
		});

		Button button = (Button) layout.findViewById(R.id.bOk);
		button.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
	}

	public static void showLogInDialog(final Activity activity)
	{
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.login_registration_dialog, null);

		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setView(layout);
		final AlertDialog alertDialog = builder.create();

		// Add login code
		Button bLogin = (Button) layout.findViewById(R.id.bUseExistingUser);
		bLogin.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.setClass(activity, LoginActivity.class);
				activity.startActivity(intent);
				alertDialog.dismiss();
			}
		});

		Button bRegister = (Button) layout.findViewById(R.id.bRegister);
		bRegister.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.setClass(activity, RegisterActivity.class);
				activity.startActivity(intent);
				alertDialog.dismiss();
			}
		});

		alertDialog.show();
	}

	public static void showAddCommentDialog(final Activity activity, final RecipeComment comment, final long recipeId, final Action onAddCommentListener)
	{
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.new_comment_view, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setView(layout);
		final AlertDialog alertDialog = builder.create();

		final EditText editText = (EditText) layout.findViewById(R.id.edtTitle);
		final Boolean isModify = comment != null;
		if (isModify) editText.setText(comment.Comment);

		Button bOk = (Button) layout.findViewById(R.id.bOk);
		bOk.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String commentTxt = editText.getText().toString();
				if (commentTxt.trim().equalsIgnoreCase(""))
				{
					alertDialog.dismiss();
					return;
				}
				if (isModify)
					Helpers.getDataHelper().RecipesRepository.editRecipeComment(comment.ID, commentTxt);
				else
					Helpers.getDataHelper().RecipesRepository.addRecipeComment(recipeId, commentTxt);
				alertDialog.dismiss();
				if (onAddCommentListener != null) onAddCommentListener.Execute();
			}
		});

		Button bCancel = (Button) layout.findViewById(R.id.bCancel);
		bCancel.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
	}

	public static void showRatingDialog(final Activity activity, final Recipe recipe)
	{
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.recipe_rating_dialog, null);

		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setView(layout);
		final AlertDialog alertDialog = builder.create();
		alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		final RatingBar ratingBar = (RatingBar) layout.findViewById(R.id.rbRecipeRating);
		ratingBar.setRating((float) recipe.getRating());
		Button button = (Button) layout.findViewById(R.id.bOk);
		button.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				recipe.setRating(ratingBar.getRating());
				Helpers.getDataHelper().RecipesRepository.setRecipeRating(recipe.getId(), recipe.getRating());
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
	}

}
